<?php

namespace app\admin\model\mental;

use think\Model;


class Invitationcode extends Model
{

    

    

    // 表名
    protected $name = 'mental_invitationcode';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [

    ];
    

    

    public static function getInvitationCode($phone)
    {
        static  $codeArr = [
            'L', '1', '2', 'C', '4', 'U',
            '6', '7', '8', '9', 'Y', 'Z',
            'A', 'B', '3', 'D', 'E', 'F',
            'G', 'H', 'I', '0', 'J', 'K',
            'M', 'N', 'O', 'P', 'Q', 'R',
            'S', 'T', '5', 'V', 'W', 'X',
        ];
        $code = '';
        while ($phone) {
            $mod = $phone % 36;
            $phone = (int)($phone / 36);
            $code = $codeArr[$mod] . $code;
        }
        $isCode = self::where('invitation_code', $code)->find();
        if ($isCode) {
            return self::getInvitationCode($phone);
        }
        return $code;
    }





    public function user()
    {
        return $this->belongsTo('app\admin\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function testmain()
    {
        return $this->belongsTo('TestMain', 'main_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
